function fileExists(path)
  local file = io.open(path, "rb")
  if file then file:close() end
  return file ~= nil
end

function printTable(t, indent)
    local textTable = ""
    local indent = indent or 0
    
    for k, v in pairs(t) do
        local ind = string.rep(" ", indent)
        if (type(k) == "number") then textTable = textTable .. ind .. "[" .. tostring(k) .. "] = "
        else textTable = textTable .. ind .. "[\"" .. tostring(k) .. "\"] = " end
        
        if (type(v) == "table") then
            textTable = textTable .. "{\n" .. printTable(v, indent+4) .. ind .. "}"
        else
            if (type(v) == "string") then textTable = textTable .. "\"" .. tostring(v) .. "\""
            else textTable = textTable .. tostring(v) end
        end
        
        textTable = textTable .. ",\n"
    end
    
    return textTable
end


if (not fileExists("data-v1.lua")) then
    print("Data file missing!")
    return
end

dofile("data-v1.lua")
if (fileExists("output.lua")) then dofile("output.lua") end

local importData = SchoolDB["namespaces"]["Teacher"]

local exportTeachers = teachers
local exportCourses = courses

if (not importData) then
    print("No data found!")
    return
end

local importClasses = importData["classes"]
local importAccount = importData["account"]
local importSpells = importAccount["spells"]

local tempTeachers = {}
local tempCourses = {}

if (importClasses ~= nil) then
    for k, v in pairs(importClasses) do
        if (v ~= nil) then
            for vk, vv in pairs(v) do
                if (vv ~= nil) then
                    for vvk, vvv in pairs(vv) do
                        if (vvv ~= nil) then
                            vvv.class = k
                            table.insert(tempTeachers, vvv)
                        end
                    end
                end
            end
        end
    end
end

if (importSpells ~= nil) then
    for k, v in pairs(importSpells) do
        if (v ~= nil) then
            for vk, vv in pairs(v) do
                if (vv ~= nil) then
                    for vvk, vvv in pairs(vv) do
                        if (vvv ~= nil) then
                            table.insert(tempCourses, vvv)
                        end
                    end
                end
            end
        end
    end
end


if (tempTeachers ~= nil) then
    if (not exportTeachers) then
        exportTeachers = {}
        for k, v in ipairs(tempTeachers) do
            if (v ~= nil) then
                table.insert(exportTeachers, v)
            end
        end
    else
        for k, v in ipairs(tempTeachers) do
            if (v ~= nil) then
                local match = false
                for lk, lv in ipairs(exportTeachers) do
                    if (lv ~= nil) then
                        if (v.class == lv.class) and (v.type == lv.type) and (v.name == lv.name) and (v.zone == lv.zone) then 
                            match = true 
                            break
                        end
                    end
                end
                
                if (not match) then
                    table.insert(exportTeachers, v)
                end
            end
        end
    end
end

if (tempCourses ~= nil) then
    if (not exportCourses) then
        exportCourses = {}
        for k, v in ipairs(tempCourses) do
            if (v ~= nil) then
                table.insert(exportCourses, v)
            end
        end
    else
        for k, v in ipairs(tempCourses) do
            if (v ~= nil) then
                local match = false
                for lk, lv in ipairs(exportCourses) do
                    if (lv ~= nil) then
                        if (v.category == lv.category) and (v.name == lv.name) and (v.rank == lv.rank) then
                            match = true
                            break
                        end
                    end
                end
                
                if (not match) then
                    table.insert(exportCourses, v)
                end
            end
        end
    end
end

local exportTeachersClass = {}
local exportTeachersType = {}
local exportTeachersName = {}
local exportTeachersZone = {}
local exportTeachersSubzone = {}

local exportTeachersClassLookup = {}
local exportTeachersTypeLookup = {}
local exportTeachersZoneLookup = {}
local exportTeachersSubzoneLookup = {}

if (exportTeachers ~= nil) then
    for k, v in ipairs(exportTeachers) do
        if (v ~= nil) then
            if (not exportTeachersClass[v.class]) then exportTeachersClass[v.class] = {} end
            table.insert(exportTeachersClass[v.class], k)
            if (not exportTeachersClassLookup[v.name]) then exportTeachersClassLookup[v.name] = {} end
            exportTeachersClassLookup[v.name] = v.class
            
            if (not exportTeachersType[v.type]) then exportTeachersType[v.type] = {} end
            table.insert(exportTeachersType[v.type], k)
            if (not exportTeachersTypeLookup[v.name]) then exportTeachersTypeLookup[v.name] = {} end
            exportTeachersTypeLookup[v.name] = v.type
            
            if (not exportTeachersName[v.name]) then exportTeachersName[v.name] = {} end
            table.insert(exportTeachersName[v.name], k)
            
            if (not exportTeachersZone[v.zone]) then exportTeachersZone[v.zone] = {} end
            table.insert(exportTeachersZone[v.zone], k)
            if (not exportTeachersZoneLookup[v.name]) then exportTeachersZoneLookup[v.name] = {} end
            exportTeachersZoneLookup[v.name] = v.zone
            
            if (not exportTeachersSubzone[v.subzone]) then exportTeachersSubzone[v.subzone] = {} end
            table.insert(exportTeachersSubzone[v.subzone], k)
            if (not exportTeachersSubzoneLookup[v.name]) then exportTeachersSubzoneLookup[v.name] = {} end
            exportTeachersSubzoneLookup[v.name] = v.subzone
        end
    end
end

local exportCoursesCategory = {}
local exportCoursesName = {}
local exportCoursesFullname = {}
local exportCoursesRank = {}
local exportCoursesLevel = {}
local exportCoursesSkill = {}
local exportCoursesCost = {}

local exportCoursesCategoryLookup = {}
local exportCoursesNameLookup = {}
local exportCoursesLevelLookup = {}
local exportCoursesCostLookup = {}

if (exportCourses ~= nil) then
    for k, v in ipairs(exportCourses) do
        if (v ~= nil) then
            if (not exportCoursesCategory[v.category]) then exportCoursesCategory[v.category] = {} end
            table.insert(exportCoursesCategory[v.category], k)
            if (not exportCoursesCategoryLookup[v.fullname]) then exportCoursesCategoryLookup[v.fullname] = {} end
            exportCoursesCategoryLookup[v.fullname] = v.category
            
            if (not exportCoursesName[v.name]) then exportCoursesName[v.name] = {} end
            table.insert(exportCoursesName[v.name], k)
            if (not exportCoursesNameLookup[v.fullname]) then exportCoursesNameLookup[v.fullname] = {} end
            exportCoursesNameLookup[v.fullname] = v.name
            
            if (not exportCoursesFullname[v.fullname]) then exportCoursesFullname[v.fullname] = {} end
            table.insert(exportCoursesFullname[v.fullname], k)
            
            if (not exportCoursesRank[v.rank]) then exportCoursesRank[v.rank] = {} end
            table.insert(exportCoursesRank[v.rank], k)
            
            if (not exportCoursesLevel[v.level]) then exportCoursesLevel[v.level] = {} end
            table.insert(exportCoursesLevel[v.level], k)
            if (exportCoursesLevelLookup[v.fullname]) then exportCoursesLevelLookup[v.fullname] = {} end
            exportCoursesLevelLookup[v.fullname] = v.level
            
            if (v.ability ~= nil) then
                for ak, av in pairs(v.ability) do
                    if (av ~= nil) then
                        if (not exportCoursesSkill[av.skill]) then exportCoursesSkill[av.skill] = {} end
                        table.insert(exportCoursesSkill[av.skill], k)
                    end
                end
            end

            if (v.cost ~= nil) and (v.cost.money ~= nil) then
                if (not exportCoursesCost[v.cost.money]) then exportCoursesCost[v.cost.money] = {} end
                table.insert(exportCoursesCost[v.cost.money], k)
                if (exportCoursesCostLookup[v.fullname]) then exportCoursesCostLookup[v.fullname] = {} end
                exportCoursesCostLookup[v.fullname] = v.cost.money
            end
        end
    end
end

local indent = 4
local indentString = string.rep(" ", indent)

function tableEntry(name, var, indent)
    local tableEntry = ""
    local indentString = ""
    if (not indent) then indent = 0 end
    if (not name) then name = time() end
    
    if (indent > 0) then indentString = string.rep(" ", indent) end
    tableEntry = tableEntry .. indentString
    
    tableEntry = tableEntry .. name .. " = {\n"
    tableEntry = tableEntry .. printTable(var, indent + 4)
    tableEntry = tableEntry .. indentString .. "}\n"
    
    return tableEntry
end

local file = io.open("output.lua", "w")

if (exportTeachers ~= nil) then
    -- file:write(indentString .. "teachers = {\n" .. printTable(exportTeachers, indent+4) .. indentString .. "}\n")
    file:write(tableEntry("teachers", exportTeachers, 4) .. "\n")
    
    -- if (exportTeachersClass ~= nil) then file:write("teachers_class = {\n" .. printTable(exportTeachersClass, 4) .. "\n}\n") end
    if (exportTeachersClass ~= nil) then file:write(tableEntry("teachers_class", exportTeachersClass, 4) .. "\n") end
    
    -- if (exportTeachersType ~= nil) then file:write("teachers_type = {\n" .. printTable(exportTeachersType, 4) .. "\n}\n") end
    if (exportTeachersType ~= nil) then file:write(tableEntry("teachers_type", exportTeachersType, 4) .. "\n") end
    
    -- if (exportTeachersName ~= nil) then file:write("teachers_name = {\n" .. printTable(exportTeachersName, 4) .. "\n}\n") end
    if (exportTeachersName ~= nil) then file:write(tableEntry("teachers_name", exportTeachersName, 4) .. "\n") end
    
    -- if (exportTeachersZone ~= nil) then file:write("teachers_zone = {\n" .. printTable(exportTeachersZone, 4) .. "\n}\n") end
    if (exportTeachersZone ~= nil) then file:write(tableEntry("teachers_zone", exportTeachersZone, 4) .. "\n") end
    
    -- if (exportTeachersSubzone ~= nil) then file:write("teachers_subzone = {\n" .. printTable(exportTeachersSubzone, 4) .. "\n}\n") end
    if (exportTeachersSubzone ~= nil) then file:write(tableEntry("teachers_subzone", exportTeachersSubzone, 4) .. "\n") end
    
    -- if (exportTeachersClassLookup ~= nil) then file:write("teachers_class_lookup = {\n" .. printTable(exportTeachersClassLookup, 4) .. "\n}\n") end
    if (exportTeachersClassLookup ~= nil) then file:write(tableEntry("teachers_class_lookup", exportTeachersClassLookup, 4) .. "\n") end
    
    -- if (exportTeachersTypeLookup ~= nil) then file:write("teachers_type_lookup = {\n" .. printTable(exportTeachersTypeLookup, 4) .. "\n}\n") end
    if (exportTeachersTypeLookup ~= nil) then file:write(tableEntry("teachers_type_lookup", exportTeachersTypeLookup, 4) .. "\n") end
    
    -- if (exportTeachersZoneLookup ~= nil) then file:write("teachers_zone_lookup = {\n" .. printTable(exportTeachersZoneLookup, 4) .. "\n}\n") end
    if (exportTeachersZoneLookup ~= nil) then file:write(tableEntry("teachers_zone_lookup", exportTeachersZoneLookup, 4) .. "\n") end
    
    -- if (exportTeachersSubzoneLookup ~= nil) then file:write("teachers_subzone_lookup = {\n" .. printTable(exportTeachersSubzoneLookup, 4) .. "\n}\n") end
    if (exportTeachersSubzoneLookup ~= nil) then file:write(tableEntry("teachers_subzone_lookup", exportTeachersSubzoneLookup, 4) .. "\n") end
end

if (exportCourses ~= nil) then
    -- file:write("courses = {\n" .. printTable(exportCourses, 8) .. "\n}\n")
    file:write(tableEntry("courses", exportCourses, 4) .. "\n")
        
    -- if (exportCoursesCategory ~= nil) then file:write("courses_category = {\n" .. printTable(exportCoursesCategory, 4) .. "\n}\n") end
    if (exportCoursesCategory ~= nil) then file:write(tableEntry("courses_category", exportCoursesCategory, 4) .. "\n") end
    -- if (exportCoursesName ~= nil) then file:write("courses_name = {\n" .. printTable(exportCoursesName, 4) .. "\n}\n") end
    if (exportCoursesName ~= nil) then file:write(tableEntry("courses_name", exportCoursesName, 4) .. "\n") end
    -- if (exportCoursesFullname ~= nil) then file:write("courses_fullname = {\n" .. printTable(exportCoursesFullname, 4) .. "\n}\n") end
    if (exportCoursesFullname ~= nil) then file:write(tableEntry("courses_fullname", exportCoursesFullname, 4) .. "\n") end
    -- if (exportCoursesRank ~= nil) then file:write("courses_rank = {\n" .. printTable(exportCoursesRank, 4) .. "\n}\n") end
    if (exportCoursesRank ~= nil) then file:write(tableEntry("courses_rank", exportCoursesRank, 4) .. "\n") end
    -- if (exportCoursesLevel ~= nil) then file:write("courses_level = {\n" .. printTable(exportCoursesLevel, 4) .. "\n}\n") end
    if (exportCoursesLevel ~= nil) then file:write(tableEntry("courses_level", exportCoursesLevel, 4) .. "\n") end
    -- if (exportCoursesSkill ~= nil) then file:write("courses_skill = {\n" .. printTable(exportCoursesSkill, 4) .. "\n}\n") end
    if (exportCoursesSkill ~= nil) then file:write(tableEntry("courses_skill", exportCoursesSkill, 4) .. "\n") end
    -- if (exportCoursesCost ~= nil) then file:write("courses_cost = {\n" .. printTable(exportCoursesCost, 4) .. "\n}\n") end
    if (exportCoursesCost ~= nil) then file:write(tableEntry("courses_cost", exportCoursesCost, 4) .. "\n") end
    
    -- if (exportCoursesCategoryLookup ~= nil) then file:write("courses_category_lookup = {\n" .. printTable(exportCoursesCategoryLookup, 4) .. "\n}\n") end
    if (exportCoursesCategoryLookup ~= nil) then file:write(tableEntry("courses_category_lookup", exportCoursesCategoryLookup, 4) .. "\n") end
    -- if (exportCoursesNameLookup ~= nil) then file:write("courses_name_lookup = {\n" .. printTable(exportCoursesNameLookup, 4) .. "\n}\n") end
    if (exportCoursesNameLookup ~= nil) then file:write(tableEntry("courses_name_lookup", exportCoursesNameLookup, 4) .. "\n") end
    -- if (exportCoursesLevelLookup ~= nil) then file:write("courses_level_lookup = {\n" .. printTable(exportCoursesLevelLookup, 4) .. "\n}\n") end
    if (exportCoursesLevelLookup ~= nil) then file:write(tableEntry("courses_level_lookup", exportCoursesLevelLookup, 4) .. "\n") end
    -- if (exportCoursesCostLookup ~= nil) then file:write("courses_cost_lookup = {\n" .. printTable(exportCoursesCostLookup, 4) .. "\n}\n") end
    if (exportCoursesCostLookup ~= nil) then file:write(tableEntry("courses_cost_lookup", exportCoursesCostLookup, 4) .. "\n") end
end

file:close()

print("Teachers")
print("========")
print("Data: " .. #exportTeachers)
if (exportTeachersClass ~= nil) then print("Class: " .. #exportTeachersClass) end
if (exportTeachersType ~= nil) then print("Type: " .. table.maxn(exportTeachersType)) end
if (exportTeachersName ~= nil) then print("Name: " .. table.maxn(exportTeachersName)) end
if (exportTeachersZone ~= nil) then print("Zone: " .. table.maxn(exportTeachersZone)) end
if (exportTeachersSubzone ~= nil) then print("Subzone: " .. table.maxn(exportTeachersSubzone)) end
if (exportTeachersClassLookup ~= nil) then print("Lookup - Class: " .. table.maxn(exportTeachersClassLookup)) end
if (exportTeachersTypeLookup ~= nil) then print("Lookup - Type: " .. table.maxn(exportTeachersTypeLookup)) end
if (exportTeachersZoneLookup ~= nil) then print("Lookup - Zone: " .. table.maxn(exportTeachersZoneLookup)) end
if (exportTeachersSubzoneLookup ~= nil) then print("Lookup - Subzone: " .. table.maxn(exportTeachersSubzoneLookup)) end
print(" ")

print("Courses")
print("=======")
print("Data: " .. #exportCourses)
if (exportCoursesCategory ~= nil) then print("Category: " .. tostring(#exportCoursesCategory)) end
if (exportCoursesName ~= nil) then print("Name: " .. table.maxn(exportCoursesName)) end
if (exportCoursesFullname ~= nil) then print("Fullname: " .. table.maxn(exportCoursesFullname)) end
if (exportCoursesRank ~= nil) then print("Rank: " .. table.maxn(exportCoursesRank)) end
if (exportCoursesLevel ~= nil) then print("Level: " .. table.maxn(exportCoursesLevel)) end
if (exportCoursesSkill ~= nil) then print("Skill: " .. table.maxn(exportCoursesSkill)) end
if (exportCoursesCost ~= nil) then print("Cost: " .. table.maxn(exportCoursesCost)) end
if (exportCoursesCategoryLookup ~= nil) then print("Lookup - Category: " .. table.maxn(exportCoursesCategoryLookup)) end
if (exportCoursesNameLookup ~= nil) then print("Lookup - Name: " .. table.maxn(exportCoursesNameLookup)) end
if (exportCoursesLevelLookup ~= nil) then print("Lookup - Level: " .. table.maxn(exportCoursesLevelLookup)) end
if (exportCoursesCostLookup ~= nil) then print("Lookup - Cost: " .. table.maxn(exportCoursesCostLookup)) end
print(" ")

